package com.myopen.controller.admin;


import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseDataTableVo;
import com.myopen.base.BaseVo;
import com.myopen.config.ConstConfig;
import com.myopen.model.Attr;
import com.myopen.model.FileInfo;
import com.myopen.model.Product;
import com.myopen.model.WebsiteColumn;

public class ProductController extends BaseController{
	
	@ActionKey("/admin/shop/product")
	public void index(){
		render("/admin/shop/product/index.html");
	}

	@ActionKey("/admin/shop/product/pagequery")
	public void pagequery(){
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		Page<Record> page=Product.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@ActionKey("/admin/shop/product/add")
	public void add(){
		

		Record vo=new Record();
		vo.set("detailObj", new Record());

		setAttr("vo", vo);//有时候需要事先编一个号，这样比较方便
		setAttr(ConstConfig.ACTION_TYPE, ConstConfig.ACTION_TYPE_ADD);
		render("/admin/shop/product/detail.html");
	}

	@ActionKey("/admin/shop/product/view")
	public void view(){
		
		String id=getPara("id");

		Record vo=Product.dao.getRecordById(id);
		List<Record> attrs=Attr.dao.getListByCategoryId(vo.getStr("categoryId"));


		setAttr("vo", vo);
		setAttr("attrs", attrs);
		setAttr(ConstConfig.ACTION_TYPE, ConstConfig.ACTION_TYPE_ADD);
		render("/admin/shop/product/detail.html");
	}

	@ActionKey("/admin/shop/product/save")
	public void save(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		
		String id=getPara("id");
		String categoryId=getPara("categoryId");
		String name=getPara("name");
		String[] ext_attr_names=getParaValues("ext_attr_name");
		String[] ext_attr_values=getParaValues("ext_attr_value");
		String[] files=getPara("files").split(",");
		String[] attrids=getParaValues("attrid");//需要根据attrid获取相应的属性

		Record vo=new Record();
		vo.set("id", id);
		vo.set("categoryId", categoryId);
		vo.set("name", name);

		Record detail=new Record();
		List<Record> ext_attr=new ArrayList<>();
		List<Record> ext_img=new ArrayList<>();
		
		if(ext_attr_names!=null){
			for (int i = 0; i < ext_attr_names.length; i++) {
				String ext_attr_name=ext_attr_names[i];
				String ext_attr_value=ext_attr_values[i];
				Record r=new Record();
				r.set("ext_attr_name", ext_attr_name);
				r.set("ext_attr_value", ext_attr_value);
				ext_attr.add(r);
			}
		}
		if(files!=null){
			for (int i = 0; i < files.length; i++) {
				String fileId=files[i];
				Record r=FileInfo.dao.getRecordById(fileId);
				if(r!=null){
					ext_img.add(r);
				}
			}
		}

		detail.set("ext_attr", ext_attr);
		detail.set("ext_img", ext_img);

		List<Record> attrs=new ArrayList<>();
		for (int i = 0; i < attrids.length; i++) {
			
			String attrid=attrids[i];
			String[] attrValues=getParaValues(attrid);

			Record at=Attr.dao.getRecordById(attrid);
			if(at!=null){
				List<Record> aValues=new ArrayList<>();
				if(attrValues!=null){
					for (int j = 0; j < attrValues.length; j++) {
						Record attrValue=Attr.dao.getAttrValueRecordById(attrValues[j]);
						if(attrValue!=null){
							aValues.add(attrValue);
						}
					}
				}
	
				at.set("attrValues", aValues);
	
				attrs.add(at);
			}
			
		}

		//
		String[] skuIds=getParaValues("skuId"); 
		String[] skuCodes=getParaValues("skuCode"); 
		String[] skuStocks=getParaValues("skuStock"); 
		String[] skuPrices=getParaValues("skuPrice");
		List<Record> skus=new ArrayList<>();
		for(int i=0;i<skuIds.length;i++){
			Record r=new Record();
			r.set("skuId",skuIds[i]);
			r.set("skuCode",skuCodes[i]);
			r.set("skuStock",skuStocks[i]);
			r.set("skuPrice",skuPrices[i]);
			List<Record> attrGroup=new ArrayList<>();
			for (int j = 0; j < attrs.size(); j++) {
				Record skuAttrValue=new Record();
				String[] skuAttrValues=getParaValues(attrs.get(j).getStr("id")+"_sku_attrId");

				skuAttrValue.set("attrId",attrids[j]);
				skuAttrValue.set("attrValue",skuAttrValues[i]);
				attrGroup.add(skuAttrValue);
			}
			r.set("attrGroup", attrGroup);
			skus.add(r);
		}

		vo=Product.dao.save(currUser, vo,detail, attrs,skus);

		Record data=new Record();
		data.set("vo", vo);
		baseVo.setData(data);
		
		renderJson(baseVo);
	}
	
	@ActionKey("/admin/product/delete")
	public void delete(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteColumn.dao.remove(id,currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}

	@ActionKey("/admin/product/getAttrByCategory")
	public void getAttrByCategory(){

		BaseVo baseVo=new BaseVo();

		String categoryId=getPara("categoryId");
		List<Record> attrs=Attr.dao.getListByCategoryId(categoryId);

		baseVo.setCode("0");
		baseVo.setData(attrs);

		renderJson(baseVo);

	}

	/**
	 * 商品上架
	 */
	@ActionKey("/admin/shop/product/publish")
	public void startUsing(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		Product.dao.changeStatus(id, "1", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	/**
	 * 商品下架
	 */
	@ActionKey("/admin/shop/product/unpublish")
	public void forbidden(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		Product.dao.changeStatus(id, "0", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}

}
